package android.ijiajiao.control;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.catalina.core.ApplicationPart;

import com.google.gson.Gson;

import android.ijiajiao.domain.Article;
import android.ijiajiao.domain.Complain;
import android.ijiajiao.domain.Contract;
import android.ijiajiao.domain.FamousTeacher;
import android.ijiajiao.domain.Friend;
import android.ijiajiao.domain.Identi;
import android.ijiajiao.domain.Learn;
import android.ijiajiao.domain.User;
import android.ijiajiao.domain.Video;
import android.ijiajiao.server.impl.ArticleServerImpl;
import android.ijiajiao.server.impl.ComplainServerImpl;
import android.ijiajiao.server.impl.ContractServerImpl;
import android.ijiajiao.server.impl.FamousTeacherServerImpl;
import android.ijiajiao.server.impl.FriendServerImpl;
import android.ijiajiao.server.impl.LearnServerImpl;
import android.ijiajiao.server.impl.UserServerImpl;
import android.ijiajiao.server.impl.VideoServerImpl;
import android.ijiajiao.server.impl.identiServerImpl;
import android.ijiajiao.util.ActionContentUtil;
import android.ijiajiao.util.GetDataUtil;
import android.ijiajiao.util.GsonInstanceUtil;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class ControlServlet
 */
@MultipartConfig
@WebServlet("/ControlServlet")
public class ControlServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码格式
		request.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");

		// 接收action
		String action = request.getParameter("action");

		String data = new GetDataUtil().getData(request.getInputStream());

		System.out.println(data + "-" + action);
		// 设置返回的数据responsedata，格式：(json)
		JSONObject ResponseData = null;
		// 判断用户名是否可用action=10001
		if (ActionContentUtil.REGISTERNAME.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			User user = g.fromJson(data, User.class);
			ResponseData = new UserServerImpl().doRegisterName(user.getName());
			response.getWriter().println(ResponseData);
		}
		// 判断email是否可用action=10002
		if (ActionContentUtil.REGISTEREMAIL.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			User user = g.fromJson(data, User.class);
			ResponseData = new UserServerImpl().doRegisterEmail(user.getEmail());
			response.getWriter().println(ResponseData);
		}
		// 注册action=10003
		if (ActionContentUtil.REGISTER.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			User user = g.fromJson(data, User.class);
			ResponseData = new UserServerImpl().doRegister(user.getName(), user.getEmail(), user.getPass(),
					user.getStatus());
			response.getWriter().println(ResponseData);
		}
		// 登陆action=10004
		if (ActionContentUtil.LOAD.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			User user = g.fromJson(data, User.class);
			ResponseData = new UserServerImpl().doLogin(user.getEmail(), user.getPass());
			response.getWriter().println(ResponseData);
		}
		// 找回密码action=10005
		if (ActionContentUtil.FINDPASS.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			User user = g.fromJson(data, User.class);
			ResponseData = new UserServerImpl().doFindPass(user.getEmail());
			response.getWriter().println(ResponseData);
		}
		// 精彩文章列表action=10006
		if (ActionContentUtil.ARTICLELIST.equals(action)) {
			ResponseData = new ArticleServerImpl().ArticleList();
			response.getWriter().println(ResponseData);
		}
		// 添加精彩文章action=10007
		if (ActionContentUtil.ADDARTICLE.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Article article = g.fromJson(data, Article.class);
			ResponseData = new ArticleServerImpl().addArticle(article.getArti_autor(), article.getArti_content(),
					article.getArti_title());
			response.getWriter().println(ResponseData);
		}

		// 精彩文章详细信息action=10008
		if (ActionContentUtil.ARTICLEDETAIL.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Article article = g.fromJson(data, Article.class);
			ResponseData = new ArticleServerImpl().ArticleDetail(article.getArti_id());
			response.getWriter().println(ResponseData);
		}
		// 名师列表action=10009
		if (ActionContentUtil.FAMOUSTEACHERLIST.equals(action)) {
			ResponseData = new FamousTeacherServerImpl().famousTeacherList();
			response.getWriter().println(ResponseData);
		}
		// 名师详情action=10010
		if (ActionContentUtil.FAMOUSTEACHERDATILE.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			FamousTeacher famousTeacher = g.fromJson(data, FamousTeacher.class);
			ResponseData = new FamousTeacherServerImpl().famousTeacherDetail(famousTeacher.getName());
			response.getWriter().println(ResponseData);
		}
		// 修改头像action=10011
		if (ActionContentUtil.UPDATAUSERIMG.equals(action)) {
			String dataa = request.getParameter("data");

			Gson g = GsonInstanceUtil.getInstanceGson();
			User user = g.fromJson(dataa, User.class);
			String path = request.getServletContext().getRealPath("data/user_img");
			// Part part = request.getPart("file");
			ApplicationPart part = (ApplicationPart) request.getPart("file");
			ResponseData = new UserServerImpl().doMotifyUserImg(user.getName(), part, path);
			response.getWriter().println(ResponseData);
		}

		// 用户资料修改 action= 10012
		if (ActionContentUtil.UPDATAUSERDATA.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			User user = g.fromJson(data, User.class);
			ResponseData = new UserServerImpl().doMotifyUserData(user.getName(), user.getSex(), user.getAge(),
					user.getPhone(), user.getQq(), user.getAdr());
			response.getWriter().println(ResponseData);
		}

		// 修改密码action=10013
		if (ActionContentUtil.UPDATAPASS.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			User user = g.fromJson(data, User.class);
			ResponseData = new UserServerImpl().doMotifyPass(user.getName(), user.getPass(), user.getPassed());
			response.getWriter().println(ResponseData);
		}

		// 用户资料action=10014
		if (ActionContentUtil.USERDATA.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			User user = g.fromJson(data, User.class);
			ResponseData = new UserServerImpl().doUserData(user.getName());
			response.getWriter().println(ResponseData);
		}
		// 收藏文章action=10015
		if (ActionContentUtil.COLLECTARTICLE.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Article article = g.fromJson(data, Article.class);
			ResponseData = new ArticleServerImpl().collectArti(article.getName(), article.getArti_id());
			response.getWriter().println(ResponseData);
		}

		// 收藏学习方法action=10016
		if (ActionContentUtil.COLLECTLEARN.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Learn learn = g.fromJson(data, Learn.class);
			ResponseData = new LearnServerImpl().collectLearn(learn.getName(), learn.getLearn_id());
			System.out.println(ResponseData);
			response.getWriter().println(ResponseData);
		}
		// 收藏视频action=10017
		if (ActionContentUtil.COLLECTVIDEO.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Video video = g.fromJson(data, Video.class);
			ResponseData = new VideoServerImpl().collectVideo(video.getName(), video.getVideo_id());
			response.getWriter().println(ResponseData);
		}
		// 添加视频action=10018
		if (ActionContentUtil.ADDVIDEO.equals(action)) {
			String datat = request.getParameter("data");
			Gson g = GsonInstanceUtil.getInstanceGson();
			Video video = g.fromJson(datat, Video.class);
			String path = request.getServletContext().getRealPath("data/video");
			Part part = request.getPart("file");
			System.out.println(part);
			ResponseData = new VideoServerImpl().AddVideo(video.getVideo_title(), video.getVideo_autor(),
					video.getVideo_summary(), part, path);
			response.getWriter().println(ResponseData);
		}
		// 视频列表action=10019
		if (ActionContentUtil.VIDEOLIST.equals(action)) {
			ResponseData = new VideoServerImpl().VideoList();
			response.getWriter().println(ResponseData);
		}

		// 视频详情action=10020
		if (ActionContentUtil.VIDEODETAIL.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Video video = g.fromJson(data, Video.class);
			ResponseData = new VideoServerImpl().VideoDetail(video.getVideo_id());
			response.getWriter().println(ResponseData);
		}

		// 学习方法列表action=10021
		if (ActionContentUtil.LEARNLIST.equals(action)) {
			ResponseData = new LearnServerImpl().doLearnList();
			response.getWriter().println(ResponseData);
		}

		// 添加学习方法action=10022
		if (ActionContentUtil.ADDLEARN.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Learn learn = g.fromJson(data, Learn.class);
			ResponseData = new LearnServerImpl().doAddLearn(learn.getLearn_autor(), learn.getLearn_content(),
					learn.getLearn_title());
			response.getWriter().println(ResponseData);
		}

		// 学习方法详情action=10023
		if (ActionContentUtil.LEARNDETAIL.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Learn learn = g.fromJson(data, Learn.class);
			ResponseData = new LearnServerImpl().doLearnDetail(learn.getLearn_id());
			response.getWriter().println(ResponseData);
		}

		// 收藏的文章action=10024
		if (ActionContentUtil.COLLECTEDARTICLE.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Article article = g.fromJson(data, Article.class);
			ResponseData = new ArticleServerImpl().collectedArti(article.getName());
			response.getWriter().println(ResponseData);

		}
		// 收藏的学习方法action=10025
		if (ActionContentUtil.COLLECTEDLEARN.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Learn learn = g.fromJson(data, Learn.class);
			ResponseData = new LearnServerImpl().collectedLearn(learn.getName());
			response.getWriter().println(ResponseData);

		}
		// 收藏的视频action=10026
		if (ActionContentUtil.COLLECTEDVIDEO.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Video video = g.fromJson(data, Video.class);
			ResponseData = new VideoServerImpl().collectedVideo(video.getName());
			response.getWriter().println(ResponseData);
		}
		// 签约列表action=10027
		if (ActionContentUtil.CONTRACTLIST.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Contract contract = g.fromJson(data, Contract.class);
			ResponseData = new ContractServerImpl().contractList(contract.getName());
			response.getWriter().println(ResponseData);
		}

		// 签约动作action=10028
		if (ActionContentUtil.CONTRACT.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Contract contract = g.fromJson(data, Contract.class);
			ResponseData = new ContractServerImpl().contract(contract.getName(), contract.getContract_id());
			response.getWriter().println(ResponseData);
		}
		// 投诉与建议action=10029
		if (ActionContentUtil.COMPLAIN.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Complain complain = g.fromJson(data, Complain.class);
			ResponseData = new ComplainServerImpl().complain(complain.getName(), complain.getContent());
			response.getWriter().println(ResponseData);
		}
		// 用户认证action=10030
		if (ActionContentUtil.IDENTI.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(data, Identi.class);
			ResponseData = new identiServerImpl().identi(identi.getName(), identi.getSummary());
			response.getWriter().println(ResponseData);
		}
		// // 身份证正面action=10031
		if (ActionContentUtil.REALNAME.equals(action)) {
			String datat = request.getParameter("data");
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(datat, Identi.class);
			String path = request.getServletContext().getRealPath("data/identi");
			Part part = request.getPart("file");
			ResponseData = new identiServerImpl().realName(identi.getName(), path, part);
			response.getWriter().println(ResponseData);
		}

		// // 身份证反面action=10032
		if (ActionContentUtil.REALMENABACK.equals(action)) {
			String datat = request.getParameter("data");
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(datat, Identi.class);
			String path = request.getServletContext().getRealPath("data/identi");
			Part part = request.getPart("file");
			ResponseData = new identiServerImpl().realNameBack(identi.getName(), path, part);
			response.getWriter().println(ResponseData);
		}
		// 学历证书action=10033
		if (ActionContentUtil.EDU.equals(action)) {
			String datat = request.getParameter("data");
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(datat, Identi.class);
			String path = request.getServletContext().getRealPath("data/identi");
			Part part = request.getPart("file");
			ResponseData = new identiServerImpl().edu(identi.getName(), path, part);
			response.getWriter().println(ResponseData);
		}
		// 教师资格证action=10034
		if (ActionContentUtil.TEACHER.equals(action)) {
			String datat = request.getParameter("data");
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(datat, Identi.class);
			String path = request.getServletContext().getRealPath("data/identi");
			Part part = request.getPart("file");
			ResponseData = new identiServerImpl().teacher(identi.getName(), path, part);
			response.getWriter().println(ResponseData);
		}
		// 奖状action=10035
		if (ActionContentUtil.QUALI.equals(action)) {
			String datat = request.getParameter("data");
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(datat, Identi.class);
			String path = request.getServletContext().getRealPath("data/identi");
			Part part = request.getPart("file");
			ResponseData = new identiServerImpl().quali(identi.getName(), path, part);
			response.getWriter().println(ResponseData);
		}
		// 身份证正面action=10036
		if (ActionContentUtil.GETREALNAME.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(data, Identi.class);
			ResponseData = new identiServerImpl().realName(identi.getName());
			response.getWriter().println(ResponseData);
		}

		// 身份证反面action=10037
		if (ActionContentUtil.GETREALMENABACK.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(data, Identi.class);
			ResponseData = new identiServerImpl().realNameBack(identi.getName());
			response.getWriter().println(ResponseData);
		}
		// 学历证书action=10038
		if (ActionContentUtil.GETEDU.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(data, Identi.class);
			ResponseData = new identiServerImpl().edu(identi.getName());
			response.getWriter().println(ResponseData);
		}
		// 教师资格证action=10039
		if (ActionContentUtil.GETTEACHER.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(data, Identi.class);
			ResponseData = new identiServerImpl().teacher(identi.getName());
			response.getWriter().println(ResponseData);
		}
		// 奖状action=10040
		if (ActionContentUtil.GETQUALI.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(data, Identi.class);
			ResponseData = new identiServerImpl().quali(identi.getName());
			response.getWriter().println(ResponseData);
		}

		// 教师认证action=10041
		if (ActionContentUtil.GETIDENTI.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Identi identi = g.fromJson(data, Identi.class);
			ResponseData = new identiServerImpl().identi(identi.getName());
			response.getWriter().println(ResponseData);
		}

		// 添加好友action=10042
		if (ActionContentUtil.ADDFRIEND.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Friend friend = g.fromJson(data, Friend.class);
			ResponseData = new FriendServerImpl().addFriend(friend.getName(), friend.getFriend_name());
			response.getWriter().println(ResponseData);
		}

		// 好友列表action=10043
		if (ActionContentUtil.FRIENDLIST.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Friend friend = g.fromJson(data, Friend.class);
			ResponseData = new FriendServerImpl().friend(friend.getName());
			response.getWriter().println(ResponseData);
		}
		// 查找好友action=10044
		if (ActionContentUtil.FINDFRIEND.equals(action)) {
			Gson g = GsonInstanceUtil.getInstanceGson();
			Friend friend = g.fromJson(data, Friend.class);
			ResponseData = new FriendServerImpl().findfriend(friend.getName());
			response.getWriter().println(ResponseData);
		}
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

}
